﻿module Sustainalytics.EsgRatings {

    export class EsgThemeOverallPerformanceController {

        // overall performance
        public overall: EsgThemeOverallPerformanceSummaryDto;
        public overall_overall_esgPositionName: string;

        constructor(
            private $scope: IEsgThemeScope,
            private $timeout: ng.ITimeoutService,
            private $q: ng.IQService,
            private EsgThemeOverallPerformanceSummaryService: IEsgThemeOverallPerformanceSummaryService,
            private $rootScope) {

            var ctx = this;

            ctx.init();
        }

        private init(): ng.IPromise<any> {

            var ctx = this;

            var promise = ctx.EsgThemeOverallPerformanceSummaryService.get(ctx.$scope.currentCompany.id, ctx.$scope.currentTheme);

            promise.then(
                // success
                function (data) {

                    ctx.$timeout(function () {
                        ctx.$scope.$apply(function () {
                            ctx.overall = data;
                            ctx.overall_overall_esgPositionName = ctx.getEsgPositionName(data.overall.esgPosition);
                        });
                    });
                },() => {

                    ga('create', 'UA-4379897-6', {
                        'userId': ctx.$rootScope.userInfo.userId, 'cookieDomain': 'none', 'name': 'globalAccess'
                    });

                    ga('globalAccess.send', 'exception', { 'exDescription': 'Esg Ratings: Overall Performance Controller -> Error initializing' });
                });

            return promise;
        }

        public getEsgPosition(): any {
            return EsgPositionEnum;
        }

        public getEsgPositionName(esgPosition: EsgPositionEnum): string {
            return EsgUtility.getEsgPositionName(esgPosition);
        }
    }


    export var esgThemeOverallPerformanceControllerName = 'EsgThemeOverallPerformanceController';

    angular.module(esgRatingsModuleName).controller(esgThemeOverallPerformanceControllerName, EsgThemeOverallPerformanceController);
} 